package com.qixin.modules.eaStandard.service.impl;

import com.qixin.modules.eaBasic.util.ChangeIdToNameUtil;
import com.qixin.modules.eaStandard.entity.EaAirplanestandard;
import com.qixin.modules.eaStandard.entity.EaStandardsetting;
import com.qixin.modules.eaStandard.mapper.EaAirplanestandardMapper;
import com.qixin.modules.eaStandard.mapper.EaStandardsettingMapper;
import com.qixin.modules.eaStandard.service.IEaAirplanestandardService;
import org.jeecg.common.util.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 飞机标准
 * @Author: qixin-boot
 * @Date: 2021-07-17
 * @Version: V1.0
 */
@Service
public class EaAirplanestandardServiceImpl extends ServiceImpl<EaAirplanestandardMapper, EaAirplanestandard> implements IEaAirplanestandardService {
    @Autowired
    private EaAirplanestandardMapper eaAirplanestandardMapper;
    @Autowired
    private ChangeIdToNameUtil changeIdToNameUtil;

    /**
     * 保存
     *
     * @param eaAirplanestandard
     * @return map
     */
    public Map<String, Object> saveObject(EaAirplanestandard eaAirplanestandard) {
        Map<String, Object> returnMap = new HashMap<String, Object>();

        // 通过组织和级别 判断是否已经存在 控制标准
        String companycode = EmptyUtil.checkNull(eaAirplanestandard.getCompanycode()); // 组织
        String level = EmptyUtil.checkNull(eaAirplanestandard.getLevel());// 级别

        //查询报销控制标准
        //部门职务关联
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("companycode", companycode);
        params.put("level", level);
        List<EaAirplanestandard> eaAirplanestandardList = eaAirplanestandardMapper.selectByMap(params);
        if (eaAirplanestandardList.size() > 0) {
            returnMap.put("code", "-1");
            returnMap.put("message", "该组织" + changeIdToNameUtil.getNameByDepartId(companycode) + "对应的级别已经维护报销控制标准,不要重复维护!");
            return returnMap;
        } else {
            eaAirplanestandardMapper.insert(eaAirplanestandard);
            returnMap.put("code", "0");
            returnMap.put("message", "新增成功!");
            return returnMap;
        }
    }

    /**
     * 编辑
     *
     * @param eaAirplanestandard
     * @return map
     */
    public Map<String, Object> updateByIdObject(EaAirplanestandard eaAirplanestandard) {
        Map<String, Object> returnMap = new HashMap<String, Object>();

        //判断是否已经存在(相应组织/级别)报销控制标准
        String companycode = EmptyUtil.checkNull(eaAirplanestandard.getCompanycode());    //组织
        String level = EmptyUtil.checkNull(eaAirplanestandard.getLevel());                  //级别

        //查询报销控制标准
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("companycode", companycode);
            params.put("level", level);
            List<EaAirplanestandard> eaAirplanestandardList = eaAirplanestandardMapper.selectByMap(params);
            if (eaAirplanestandardList.size() > 0) {
                //查询当前标准
                EaAirplanestandard eaAirplanestandard1 = eaAirplanestandardMapper.selectById(eaAirplanestandard.getId());
                if (eaAirplanestandard1.getCompanycode().equals(companycode) && eaAirplanestandard1.getLevel().equals(level) ) {  //如果当前标准维护的组织和级别没变,返回成功
                    eaAirplanestandardMapper.updateById(eaAirplanestandard);
                    returnMap.put("code", "0");
                    returnMap.put("message", "编辑成功!");
                    return returnMap;
                } else {
                    returnMap.put("code", "-1");
                    returnMap.put("message", "该组织:" + changeIdToNameUtil.getNameByDepartId(companycode) + "对应的级别已经维护报销控制标准,不要重复维护!");
                    return returnMap;
                }
            } else {
                eaAirplanestandardMapper.updateById(eaAirplanestandard);
                returnMap.put("code", "0");
                returnMap.put("message", "编辑成功!");
                return returnMap;
            }

    }
}
